package cn.da.shuai.cool.finance.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.finance.model.CoolAccountsPayable;
import cn.da.shuai.cool.finance.service.ICoolAccountsPayableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/accounts-payable")
@Tag(name = "应付账款管理")
public class CoolAccountsPayableController {

    @Autowired
    private ICoolAccountsPayableService accountsPayableService;

    @PostMapping("/record")
    @Operation(summary = "记录应付账款")
    public R<Boolean> recordPayable(@RequestBody CoolAccountsPayable payable) {
        return R.ok(accountsPayableService.recordPayable(payable));
    }

    @PostMapping("/payment")
    @Operation(summary = "记录付款")
    public R<Boolean> recordPayment(@RequestParam String invoiceNo, @RequestParam BigDecimal amount, @RequestParam String paymentReference) {
        return R.ok(accountsPayableService.recordPayment(invoiceNo, amount, paymentReference));
    }

    @GetMapping("/overdue")
    @Operation(summary = "获取逾期应付账款")
    public R<List<CoolAccountsPayable>> getOverduePayables() {
        return R.ok(accountsPayableService.getOverduePayables());
    }

    @GetMapping("/aging-analysis")
    @Operation(summary = "获取账龄分析")
    public R<Map<String, Object>> getAgingAnalysis() {
        return R.ok(accountsPayableService.getAgingAnalysis());
    }

    @GetMapping("/vendor-balance")
    @Operation(summary = "获取供应商余额")
    public R<BigDecimal> getVendorBalance(@RequestParam Long vendorId) {
        return R.ok(accountsPayableService.getVendorBalance(vendorId));
    }

    @GetMapping("/trend")
    @Operation(summary = "获取应付账款趋势")
    public R<List<Map<String, Object>>> getPayablesTrend(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return R.ok(accountsPayableService.getPayablesTrend(startDate, endDate));
    }

    @GetMapping("/cash-requirements-forecast")
    @Operation(summary = "获取现金需求预测")
    public R<Map<LocalDate, BigDecimal>> getCashRequirementsForecast() {
        return R.ok(accountsPayableService.getCashRequirementsForecast());
    }

    @GetMapping("/payment-priorities")
    @Operation(summary = "获取付款优先级")
    public R<List<CoolAccountsPayable>> getPaymentPriorities() {
        return R.ok(accountsPayableService.getPaymentPriorities());
    }
}